package cn.tedu.xuecha.service.impl;

import cn.tedu.xuecha.entity.dto.MenuDTO;
import cn.tedu.xuecha.entity.pojo.Menu;
import cn.tedu.xuecha.ex.ServiceException;
import cn.tedu.xuecha.mapper.MenuMapper;
import cn.tedu.xuecha.respone.AppCode;
import cn.tedu.xuecha.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.ServerException;

/**
 * @author coinyang
 * @date 2023/4/22 13:11
 */
@Slf4j
@Service
public class MeuServiceImpl implements MenuService {
    @Autowired
    MenuMapper menuMapper;

    @Override
    public void addNewMenu(MenuDTO menuDTO) {
            String title = menuDTO.getTitle();
        int count = menuMapper.countByTitle(title);
        if(count>0){
            String message =  "添加菜单失败  标题相同";
            log.warn(message);
            throw  new ServiceException(AppCode.ERR_CONFLICT,message);
        }
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuDTO,menu);
        menu.setType(1);
        int rows = menuMapper.insertSelective(menu);
        if (rows != 1) {
            String message = "添加菜单失败失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(AppCode.ERR_INSERT, message);
        }
    }
}
